﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LP580.CG
{
    /// <summary>
    /// clsCompanyPhoto 的摘要说明
    /// <summary>
    public class clsCompanyPhoto
    {
        /// <summary>
        /// 文件存放文件夹（前后无“/”）
        /// 实际存入数据库时上一层还有根路径如：/upfiles/
        /// </summary>
        public readonly string _FileSaveDir = "User_CompanyPics";

        #region 构造函数

        public clsCompanyPhoto() { }

        public clsCompanyPhoto(int prmUserID,int prmType)
        {
            clsCompanyPhoto entity = clsCompanyPhoto.GetByUserIDType(prmUserID, prmType);
            this._companyphotoid = entity._companyphotoid;
            this._userid = entity._userid;
            this._photopath = entity._photopath;
            this._phototype = entity._phototype;
        }

        #endregion

        public static clsCompanyPhoto GetByUserIDType(int prmUserID, int prmType)
        {
            clsCompanyPhoto entity = new clsCompanyPhoto();
            DBase dbo = new DBase();
            string sql = string.Format("SELECT * FROM [tbl_CompanyPhoto] WHERE UserID={0} AND PhotoType={1}", prmUserID, prmType);
            System.Data.IDataReader dr = dbo.ExecuteReader(sql); 
            if (dr.Read())
            {
                entity = DrToClass(dr);
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return entity;
        }

        public static clsCompanyPhoto DrToClass(System.Data.IDataReader dr)
        {
            clsCompanyPhoto entity = new clsCompanyPhoto();
            int iTmp = 0;
            if (!int.TryParse(dr["CompanyPhotoID"].ToString(), out iTmp))
            {
                iTmp = 0;
            }
            entity._companyphotoid = iTmp;
            iTmp = 0;
            if (!int.TryParse(dr["UserID"].ToString(), out iTmp))
            {
                iTmp = 0;
            }
            entity._userid = iTmp;
            entity._photopath = (string)dr["PhotoPath"].ToString();
            iTmp = 0;
            if (!int.TryParse(dr["PhotoType"].ToString(), out iTmp))
            {
                iTmp = 0;
            }
            entity._phototype = iTmp;
            return entity;
        }

        #region 增删改数据库操作

        public static int Insert(clsCompanyPhoto prmEntity)
        {
            DataTableManage SaveDTM = new DataTableManage();
            string sql;
            sql = string.Format("insert into [tbl_CompanyPhoto](" +
                            " [UserID]," +
                            " [PhotoPath]," +
                            " [PhotoType]" +
            ") values (" +
                    "@UserID," +
                    "@PhotoPath," +
                    "@PhotoType" +
            ")");
            SaveDTM.Sql = sql;
            SaveDTM.AddNewParam("@UserID", prmEntity._userid);
            SaveDTM.AddNewParam("@PhotoPath", prmEntity._photopath);
            SaveDTM.AddNewParam("@PhotoType", prmEntity._phototype);
            SaveDTM.Insert();
            return 1;
        }

        public static bool Delete(clsCompanyPhoto prmEntity)
        {
            bool ret = false;
            if (prmEntity._companyphotoid > 0)
            {
                clsCompanyPhoto.DelFile(prmEntity._photopath);
                ret= Delete(prmEntity._companyphotoid);
            }
            return ret;
        }

        public static bool Delete(int prmCompanyPhotoID)
        {
            DBase dbo = new DBase();
            dbo.ExecSql("DELETE FROM [tbl_CompanyPhoto] WHERE CompanyPhotoID=" + prmCompanyPhotoID.ToString());
            dbo.CloseConn();
            dbo = null;
            return true;
        }

        public static bool Update(clsCompanyPhoto prmEntity)
        {
            DataTableManage SaveDTM = new DataTableManage();
            string sql;
            sql = "UPDATE [tbl_CompanyPhoto] SET " +
                "[UserID] = @UserID, " +
                "[PhotoPath] = @PhotoPath, " +
                "[PhotoType] = @PhotoType " +
            " WHERE CompanyPhotoID=@CompanyPhotoID";
            SaveDTM.Sql = sql;
            SaveDTM.AddNewParam("@UserID", prmEntity._userid);
            SaveDTM.AddNewParam("@PhotoPath", prmEntity._photopath);
            SaveDTM.AddNewParam("@PhotoType", prmEntity._phototype);
            SaveDTM.AddNewParam("@CompanyPhotoID", prmEntity._companyphotoid);
            SaveDTM.Update();
            return true;
        }

        public static bool Save(clsCompanyPhoto prmEntity)
        {
            if (ExistCompanyPhoto(prmEntity._userid, prmEntity._phototype))
            {
                return Update(prmEntity);
            }
            else
            {
                return Insert(prmEntity) > 0;               
            }
        }

        private static bool ExistCompanyPhoto(int prmUserID, int prmType)
        {
            bool ret = false;
            DBase dbo = new DBase();
            string sql = string.Format("SELECT CompanyPhotoID FROM [tbl_CompanyPhoto] WHERE UserID={0} AND PhotoType={1}", prmUserID, prmType);
            System.Data.IDataReader dr = dbo.ExecuteReader(sql);
            ret = dr.Read();
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return ret;
        }

        #endregion

        public static List<clsCompanyPhoto> GetCollection()
        {
            List<clsCompanyPhoto> ls = new List<clsCompanyPhoto>();
            DBase dbo = new DBase();
            string sql = "SELECT * FROM [tbl_CompanyPhoto] ORDER BY CompanyPhotoID DESC";
            System.Data.IDataReader dr = dbo.ExecuteReader(sql);
            while (dr.Read())
            {
                ls.Add(DrToClass(dr));
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return ls;
        }

        public static void DelFile(string prmPathFile)
        {
            string strRootPath = System.Web.HttpContext.Current.Server.MapPath("~/");
            strRootPath += "/" + prmPathFile;
            if (System.IO.File.Exists(strRootPath))
                System.IO.File.Delete(strRootPath);
        }

        #region 字段属性

        private int _companyphotoid = 0;
        /// <summary>
        /// 企业图片ID-自增
        /// </summary>
        public int CompanyPhotoID
        {
            get { return _companyphotoid; }
            set { _companyphotoid = value; }
        }

        private int _userid = 0;
        /// <summary>
        /// 所属用户ID
        /// </summary>
        public int UserID
        {
            get { return _userid; }
            set { _userid = value; }
        }

        private string _photopath = "";
        /// <summary>
        /// 图片保存地址
        /// </summary>
        public string PhotoPath
        {
            get { return _photopath; }
            set { _photopath = value; }
        }

        private int _phototype = 0;
        /// <summary>
        /// 类型：0-展厅LOGO,1-展厅广告牌,2-企业证书
        /// </summary>
        public int PhotoType
        {
            get { return _phototype; }
            set { _phototype = value; }
        }

        #endregion
    }
}
